package com.ihrm.company.service.Impl;

import com.ihrm.common.entity.Constants;
import com.ihrm.common.entity.Result;
import com.ihrm.common.exception.CommonException;
import com.ihrm.common.utils.IdWorker;
import com.ihrm.company.dao.CompanyDao;
import com.ihrm.company.dao.DepartmentDao;
import com.ihrm.company.service.DepartmentService;
import com.ihrm.domain.company.Company;
import com.ihrm.domain.company.Department;
import com.ihrm.domain.company.DeptListResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    private DepartmentDao departmentDao;

    @Autowired
    private CompanyDao companyDao;

    @Override
    public Integer addDept(Department department) {
        department.setId(new IdWorker().nextId() + "");
        department.setCreateTime(new Date());
        return departmentDao.addDept(department);
    }

    @Override
    public Integer update(Department department) {
        return departmentDao.update(department);
    }

    @Override
    public Department select(String id) {
        Department department = departmentDao.select(id);
        if(department == null) {
            new CommonException(Constants.FAIL_CODE, "未查询到部门");
        }
        return department;
    }

    @Override
    public Integer delete(String id) {
        return departmentDao.delete(id);
    }

    @Override
    public DeptListResult findAll(String companyId) {
        Company company = companyDao.getCompanyById(companyId);
        List<Department> departmentList = departmentDao.findAll(companyId);
        return new DeptListResult(company,departmentList);
    }

    @Override
    public Department findByCondition(String code) {
        return departmentDao.findByCondition(code);
    }
}
